using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public float speed = 2f;
    public Vector3 direction = new Vector3(0, 1, 0);

    public Vector3 enemyRotation = Vector3.zero;
    public float randomTime = 2f;
    public float timer = 0f;

    public GameObject bullet;
    public GameObject fun;
    public float bulletSpeed = 4f;

    public float fireTimeInterval = 2f;
    public float fireTimer = 0f;

    public float PH;
    public bool IsDeath = false;

    public static Enemy Enemy_Instance;

    public GameObject bomb;
    public Sprite sprite1;
    public Sprite sprite2;
    public Sprite sprite3;
    // Start is called before the first frame update
    public float randomTimes = 0f;
    public float fireTimeIntervals = 0f;
    IEnumerator e;
    Coroutine co;

    IEnumerator f;
    Coroutine cos;
    float distance;

    public Animator anim;
    public int playerKillNum;
    public GameObject damageCanvas;
    //public Transform tesTra;
    //public Transform parentTra;
    //Vector3 localPos;
    //Vector3 worldPos;
    void Awake()
    {
        Enemy_Instance = this;
    }
    void Start()
    {
        randomTime = Random.Range(0.5f, 1f);
        fireTimeInterval = Random.Range(1f, 2f);
        //Invoke("Fire", fireTimeInterval);

        e = Normal();
        co = StartCoroutine(e);

        f = Normals();
        cos = StartCoroutine(f);
        //while (true) 
        //{
        //    Enemyfire();
        //    yield return new WaitForSeconds(1);

        //}
    }

    // Update is called once per frame
    void Update()
    {

        if (GameManager.gameManager_Instance.game_State == GameState.GameOver|| GameManager.gameManager_Instance.game_State == GameState.GameWin)
        {
            Stopenemy();
        }
            //Enemyyid();
            //Enemyfire();
            if (IsDeath)
        {
            Bomb();
            GameManager.gameManager_Instance.Enemyjian();
            Destroy(this.gameObject);
        }

        //localPos = this.tesTra.localPosition;
        //worldPos = this.parentTra.TransformPoint(localPos);
    }
    private void FixedUpdate()
    {
        this.transform.Translate(direction * speed * Time.fixedDeltaTime);
        anim.SetBool("running",true);
    }

    IEnumerator Normal() 
    {
        while (true) 
        {

            Enemyyid();
            yield return new WaitForSeconds(1);
        }
    }

    IEnumerator Normals()
    {
        while (true)
        {
            Enemyfire();
            yield return new WaitForSeconds(1);

        }
    }
    void Enemyyid() 
    {
        randomTimes = Random.Range(1.75f, 2f);
        
        timer += randomTimes;

        //this.transform.Translate(direction * speed * Time.deltaTime);

        if (timer >= randomTime)
        {
            ChangeRotation();
            timer = 0;
            randomTime = Random.Range(1f, 2f);
        }
    }

    void Enemyfire() 
    {
        fireTimeIntervals = Random.Range(1.75f, 2f);
        fireTimer += fireTimeIntervals;
        if (fireTimer >fireTimeInterval)
        {
            Fire();
            fireTimer = 0;
            fireTimeInterval = Random.Range(1f, 2f);
        }
    }

    void ChangeRotation()
    {
        float direc = Random.Range(0, 9);
        if (direc < 2)
        {
            enemyRotation = new Vector3(0, 0, 0);
        }

        if (direc >= 2&&direc<5)
        {
            enemyRotation = new Vector3(0, 0, 180);
        }

        if (direc >= 5 && direc < 7)
        {
            enemyRotation = new Vector3(0, 0, 90);
        }

        if (direc >= 7 && direc <= 9)
        {
            enemyRotation = new Vector3(0, 0, -90);
        }

        this.transform.rotation = Quaternion.Euler(enemyRotation);
    }

    void Fire()
    {
        var obj = Instantiate(bullet, fun.transform.position, this.transform.rotation);
        obj.GetComponent<Bullet>().speed = bulletSpeed;
    }

    public void BeHit(float damageValue) 
    {
       
        if (PH > 0)
        {
            PH -= damageValue;   
        }

        if (PH <= 0)
        {
            IsDeath = true;
            texttime.texttime_Instance.playerKillcoins += playerKillNum;
            DamageNum damageNum = Instantiate(damageCanvas, transform.position, Quaternion.identity).GetComponent<DamageNum>();
            damageNum.ShowUIDamage(playerKillNum);
        }
        switch (PH) 
        {
            case 3:
                this.GetComponent<SpriteRenderer>().sprite = sprite1;
                anim.SetBool("running1", true);
                break;
            case 2:
                this.GetComponent<SpriteRenderer>().sprite = sprite2;
                anim.SetBool("running2", true);
                break;
            case 1:
                this.GetComponent<SpriteRenderer>().sprite = sprite3;
                anim.SetBool("running3", true);
                break;
        }
    }

    void Bomb() 
    {

        if (bomb != null)
        {
            Instantiate(bomb, this.transform.position, bomb.transform.rotation);
        }

    }

    void Stopenemy() 
    {
        StopCoroutine(co);
        StopCoroutine(cos);
        speed = 0;
    }
    private void OnMouseDown()
    {
        if (GameManager.gameManager_Instance.mjudgment == true)
        {
            float i = TwoPointDistance2D(this.transform.localPosition, Player.Player_Instance.transform.localPosition);
            if (i <= 5) { BeHit(1f); }
            Debug.Log("localPos:" + this.transform.localPosition + "worldPos:" + this.transform.TransformPoint(this.transform.localPosition) + "playerpos" + Player.Player_Instance.transform.localPosition + "juli" + i);
        }
        }
    public float TwoPointDistance2D(Vector2 p1,Vector2 p2) 
    {
        distance = Mathf.Sqrt((p1.x-p2.x)*(p1.x-p2.x)+ (p1.y - p2.y) * (p1.y- p2.y));
        return distance;
    }
}
